<template>
  <div>
    <div class="IFdiv1">稍等，判断是否支付成功中</div>
    <div class="IFdiv2">
      <el-progress :percentage="myprogress"></el-progress>
    </div>
  </div>
</template>

<script>
export default {
    name:'PxJudgeIFPay',
    data(){
      return{
        SendaxiosData:'',
        axiosCartDate_cid:[],
        myprogress:0,
      }
    },
    mounted(){
      // this.myprogress=100
      const timer = setInterval(() => {
        if(this.myprogress != 99){
          this.myprogress+=9
        }
      }, 60);
        this.$once('hook:beforeDestroy',()=>{
            clearInterval(timer)
        })
      if(sessionStorage.getItem('PayOiddate') !=null){  //如果sessionStorage里的数据存在时
          if(this.$route.params.oid != null) {   //如果路由有传来数据(到达订单详情页面),此时是没有关闭已经存在的的订单详情页面而进行的路由跳转
              if(this.$route.params.oid != sessionStorage.getItem('PayOiddate')){
              this.SendaxiosData=this.$route.params.oid
              sessionStorage.setItem('PayOiddate',JSON.stringify(this.SendaxiosData))
              }else{
                  this.SendaxiosData=JSON.parse(sessionStorage.getItem('PayOiddate'))
              }
          }
          else{
              //如果路由没有传数据(路由没有跳转)      此时解释为刷新页面
              this.SendaxiosData=JSON.parse(sessionStorage.getItem('PayOiddate'))
          }
      }else{	    //如果sessionStorage里没有数据(即路由有跳转)    此时解释为初次进入订单详情界面
        this.$router.push({
            name:'PxMain'})
      }
      this.$addr.get(`alipay/${this.SendaxiosData}/query`).then(
          response=>{
              console.log('查看是否支付的请求成功了',response)
              if(response.data.state == 4005){
                  this.$message({
                  message: response.data.message,
                  type: 'error'
                  });
              }else if(response.data.state == 4009){
                  this.$message({
                  message: response.data.message,
                  type: 'error'
                  });
              }else if(response.data.code == '40004'){
                  this.$message({
                  message: response.data.subMsg,
                  type: 'error'
                  });
                  setTimeout(() => {
                    this.$router.push({
                        name:'PxOrderDetails',
                        params:{oid:this.SendaxiosData}})
                  }, 500);
              }else{
                  this.$message({
                  message: '恭喜您支付成功!',
                  type: 'success'
                  });
                  this.$addr.get(`carts`).then(
                      response=>{
                          console.log('请求获取该用户购物车信息成功了',response)
                          for( let i=0,j=0; i<response.data.data.length; i++){
                              if(response.data.data[i].pid.toString().indexOf('20000') != -1){
                                  this.axiosCartDate_cid[j]=response.data.data[i].cid
                                  j++
                              }
                          }
                          for(let i=0 ; i<this.axiosCartDate_cid.length; i++){
                              this.$addr.get(`carts/${this.axiosCartDate_cid[i]}/delete`).then(
                                  response=>{
                                      console.log('请求清空购物车成功了',response)
                                          console.log('ali!!!paysuccess');
                                          setTimeout(() => {
                                            this.$router.push({
                                                name:'PxOrderDetails',
                                                params:{oid:this.SendaxiosData}})
                                          }, 500);

                                      },
                                  error=>{
                                      console.log('请求清空购物车失败了',error.message)
                                  })
                          }
                      },
                      error=>{
                          console.log('请求获取该用户购物车信息商品详情失败了',error.message)
                      }
                  )
              }
          },
          error=>{
              console.log('查看是否支付的请求失败了',error.message)
          }
      )
    },
}
</script>

<style scoped>
.IFdiv1{
  text-align: center;
  margin-top: 100px;
}
.IFdiv2{
  text-align: center;
  margin-top: 50px;
  margin-right: 80px;
  margin-left: 80px;
}
</style>